﻿using Application.DTO;
using Application.DTO.Permission;
using Application.DTO.Syslog;
using Application.Service;
using Domain.Entity.OperationLogD;
using Infrastructure.IRepository.Permission;
using Infrastructure.Repository;
using Infrastructure.Repository.OperatiLog;
using Infrastructure.Repository.Permission;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Intrinsics.X86;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Infrastructure.IRepository.Base
{
    /// <summary>
    /// 操作日志服务
    /// </summary>
    public class OperationLogService : BaseService<OperationLog>,IOperationLogService
    {
        private readonly IBaseRepository<OperationLog> _logRepository;
   
        private readonly IUnitOfWork unitOfWork;

      
        public OperationLogService(IUnitOfWork unitOfWork, IBaseRepository<OperationLog> logRepository) : base(unitOfWork, logRepository)
        {
           this._logRepository = logRepository;
            this.unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 日志添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDTO<string>> OperationLogAdd(OperationLogAddDTO dto)
        {
            //开启事务
            var tran = unitOfWork.GetDbContext().Database.BeginTransaction();
            try
            {
                //添加权限信息
                await _logRepository.Insert(dto.OperationLog);
                await unitOfWork.SaveChanges();
                //提交事务
                tran.Commit();
                return new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.SUCCESS,
                    Message = "添加成功",
              
                };
            }
            catch (Exception)
            {
                //出错回滚事务
                tran.Rollback();
                return new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.SUCCESS,
                    Message = "添加失败",
                    Data = "",
                };
            }
        }
        /// <summary>
        /// 日志的倒序显示+分页+内容查询
        /// </summary>
        /// <param name="Pagesize"></param>
        /// <param name="Pageindex"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public async Task<PageDTO<OperationLogDTO>> OperationLogReverseOrder(int Pagesize, int Pageindex, string Content = ""
            //, string StartTime = "", string EndTime = ""
            )
        {
            var data = from x in await _logRepository.GetAll()
                       select new OperationLogDTO
                       {
                           OLid = x.OLid,
                           Content = x.Content,
                           Classify = x.Classify,
                           Module = x.Module,
                           Whether = x.Whether,
                           Use = x.Use,
                           OperatorName = x.OperatorName,
                           CreateTimes = x.CreateTimes,
                       };
                if(!string.IsNullOrEmpty(Content) )
            {
                data = data.Where(x => x.Content.Contains(Content));
            }
            return new PageDTO<OperationLogDTO>
            {
                recordCount = data.Count(),
                plist = data.OrderByDescending(a => a.OLid).Skip((Pageindex - 1) * Pagesize).Take(Pagesize)
            };

        }


        /// <summary>
        /// 日志的显示+分页+内容查询
        /// </summary>
        /// <param name="Pagesize"></param>
        /// <param name="Pageindex"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public async Task<PageDTO<OperationLogDTO>> LogShow(int Pagesize, int Pageindex, string Content = ""
            //,string StartTime="",string EndTime=""
            )
        {
            var data = from x in await _logRepository.GetAll()
                       select new OperationLogDTO
                       {
                           OLid = x.OLid,
                           Content = x.Content,
                           Classify = x.Classify,
                           Module = x.Module,
                           Whether = x.Whether,
                           Use = x.Use,
                           OperatorName = x.OperatorName,
                           CreateTimes = x.CreateTimes,

                       };
            //日志内容模糊查询
            if (!string.IsNullOrEmpty(Content))
            {
                data = data.Where(a => a.Content.Contains(Content));
            }
            ////查询日志开始时间
            //if(!string.IsNullOrEmpty(StartTime))
            //{
            //    data = data.Where(a => a.CreateTimes>=Convert.ToDateTime(StartTime).Date);
            //}
            ////查询日志结束时间
            //if (!string.IsNullOrEmpty(StartTime))
            //{
            //    data = data.Where(a => a.CreateTimes<= Convert.ToDateTime(EndTime).Date);
            //}
            return new PageDTO<OperationLogDTO>
            {
                recordCount = data.Count(),
                plist = data.OrderBy(a => a.OLid).Skip((Pageindex - 1) * Pagesize).Take(Pagesize)
            };

        }



        /// <summary>
        /// 根据条件获取所有数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// 
        public async Task<List<OperationLog>> GetList(LogsPageRequestDto input)
        {
            var data = from x in await _logRepository.GetAll()
                       where (string.IsNullOrEmpty(input.OperatorName) || x.OperatorName.Contains(input.OperatorName))
                      && x.Classify.Equals(input.Classify) //根据分类名查询
                       orderby x.CreateTimes descending
                       select x;
            return data.ToList();




        }


        public async Task<ResultDTO<string>> Add(OperationLog operation)
        {
            //开启事务
            var tran = unitOfWork.GetDbContext().Database.BeginTransaction();
            try
            {
               
                //添加权限信息
                operation.OperatorName = operation.OperatorName.Trim();
                _logRepository.Insert(operation);
                await unitOfWork.SaveChanges();
                //提交事务
                tran.Commit();
                return new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.SUCCESS,
                    Message = "添加成功",

                };
            }
            catch (Exception)
            {
                //出错回滚事务
                tran.Rollback();
                return new ResultDTO<string>
                {
                    Code = Domain.Enum.ResultStatusCode.SUCCESS,
                    Message = "添加失败",
                    Data = "",
                };
            }
          
        }
    }
}
